package com.linkgie.galaxyframework.utils;

import java.util.Comparator;

public class VersionComparator implements Comparator<Version> {
	
	public static final VersionComparator INSTANCE = new VersionComparator();

	private VersionComparator() {
	}
	
	@Override
	public int compare(Version v1, Version v2) {
		int majorCompare = Integer.compare(v1.getMajor(), v2.getMajor());
		if (majorCompare != 0) {
			return majorCompare;
		}
		int minorCompare = Integer.compare(v1.getMinor(), v2.getMinor());
		if (minorCompare != 0) {
			return minorCompare;
		}
		int revisionCompare = Integer.compare(v1.getRevision(), v2.getRevision());
		if (revisionCompare != 0) {
			return revisionCompare;
		}
		// RELEASE 最大；其它按字符串比较；
		if (v1.isReleased() && v2.isReleased()) {
			return 0;
		}
		if (v1.isReleased()) {
			return 1;
		}
		if (v2.isReleased()) {
			return -1;
		}
		String phase1 = v1.getPhase();
		String phase2 = v2.getPhase();
		if (phase1 == null && phase2 == null) {
			return 0;
		}
		if (phase1 == null) {
			return -1;
		}
		if (phase2 == null) {
			return 1;
		}
		return phase1.compareToIgnoreCase(phase2);
	}

}
